<?php
/**
 * Script used to include secure file belongs to specific 
 * model.
 * @author Lukasz Torba
 */
try{
 defined('BASE_PATH') || define('BASE_PATH', realpath(__DIR__).'/..');
 include_once '../api/Autoload.php';
 include_once '../api/Smarty/framework_plugins/function.GetShared.php';
 
 \Api\Session::SessionRenew();
 $model = \Api\ObjectLoader::GetObjectSession();

 \Api\Config::Renew($model[\Api\ObjectLoader::C_MODULE]);
 
 //$module = \Api\ObjectLoader::GetObjectSession();
 $module = \Api\Config::$param;

 if(empty($module))
 {
    throw new \Exception('GetLocal: UNKNOWN MODULE');
 }else if(!array_key_exists('url',$_GET))
 {
    throw new \Exception('GetLocal: Missing parameters');
 }
 else
 {
    $module = $module[\Api\ObjectLoader::C_MODULE];        
    $file = smarty_function_GetShared($_GET);
    
    $module = BASE_PATH.'/'.$module.'/public/'.preg_replace('/\/shared\//','',$file);
    
    if(file_exists($module))
    {    
        preg_match("|\.([a-z0-9]{2,4})$|i", $module, $fileSuffix);
        
         switch(strtolower($fileSuffix[1]))
         {
             case "js" :
                 $mime = "application/x-javascript";
             break;
             case "css":
                    $mime = "text/css";
                break;
             case "png":
                    $mime = "image/png";
                break;
             case "jpg":
                    $mime = "image/jpeg";
                break;
         }
        
        header('Content-type: '.$mime);
        $fp = fopen($module, "r");
        echo fread($fp, filesize($module));
        fclose($fp);
    }else
    {
        throw new \Exception('GetLocal: File '.$module.' doesn\'t exists',__LINE__);
    }
 }
}
catch(Exception $e)
{
    die($e->__toString());
}
?>